<template>
  <div class="login-container">
    <div class="content">
      <div class="logo-wrap">
        <img class="iconLogo" src="../../assets/images/logo.png" alt="logo"/>
        <h2>集装箱码头业务管理系统</h2>
        <p class="describe">建设“港通四海、陆联八方，口碑天下、辉映全球”的世界一流海洋港口</p>
        <div class="copyright">版权所有：山东港口科技集团日照有限公司</div>
      </div>
      <div class="form-wrap">
        <div class="main">
          <h3>登&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;录</h3>
          <el-form
            class="login-form"
            autocomplete="off"
            :model="loginForm"
            :rules="loginRules"
            ref="loginForm"
            label-position="left"
          >
            <el-form-item prop="username">
              <span class="svg-container svg-container_login">
                <svg-icon icon-class="yonghu" />
              </span>
              <el-input
                name="username"
                type="text"
                v-model="loginForm.username"
                autocomplete="off"
                placeholder="用户名"
              />
            </el-form-item>

            <el-form-item prop="password" class="password">
              <span class="svg-container">
                <svg-icon icon-class="Key" class="key-icon" />
              </span>
              <el-input
                name="password"
                :type="passwordType"
                v-model="loginForm.password"
                autocomplete="off"
                placeholder="密码"
              />
              <span class="show-pwd" @click="showPwd">
                <svg-icon icon-class="eye" />
              </span>
            </el-form-item>
            <div>
              <el-checkbox v-model="loginForm.remember">记住密码</el-checkbox>
            </div>

            <el-button
              type="primary"
              style="width:100%;height: 40px;"
              :loading="loading"
              @click="handleLogin"
            >登 录</el-button>
          </el-form>
          <!-- <p class="login-tip">建议使用Google Chrome 39.0 版本以上浏览器在1280*768分辨率下使用本网站</p> -->
        </div>
      </div>
    </div>
  </div>
</template>

<script>
const Base64 = require('js-base64').Base64
import axios from 'axios'
import secret from '@/utils/secret.js'
import loginApi from '@/api/modules/login/index'
import store from '@/store'
// const sha256 = require('js-sha256')
export default {
  name: 'login',
  components: {
  },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入用户名'))
      }
      callback()
    }
    const validatePassword = (rule, value, callback) => {
      /* if (value.length < 6) {
          callback(new Error('The password can not be less than 6 digits'))
        } else {*/
      callback()
      //      }
    }
    return {
      packagePath: '',
      containeWith: 0,
      loginForm: {
        username: '',
        password: '',
        remember: false
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          const { username, password } = this.loginForm
          var user_secret = secret.Encrypt(username);
          var pass_secret = secret.Encrypt(password);
          const res = await loginApi.loginByUsername({
            userAccount: user_secret,
            password: pass_secret,
            projectCode: 'container'// sessionStorage.getItem('projectCode') || ''
            // password: sha256(password + 'JxGF7gjNeje5evqzHk7z5GM3OR6OSZUFbvJRnRjKzxkop3OuQ40BVRU86jjmgvT4')
          })
          if (res.code === '00000000') {
            // 储存登录信息
            document.onkeydown = null
            this.setUserInfo()
            sessionStorage.setItem('userInfo', JSON.stringify(res.data))
            sessionStorage.setItem('projectCode', res.data.projectCode)
            console.log(res.data)
            /* let aaa = {
              "id":251041056254976,
              "token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJkZXZpY2VUeXBlIjoiQ29tcHV0ZXIiLCJ1c2VyQWNjb3VudCI6IjAwMSIsImlzcyI6Inh5aiIsImV4cCI6MTY0MzEwMTI5Nn0.9VY0ptjZTaXD8AvhKz0Y4QD8YDF322b5tcHWxSzhxo8",
              "userAccount":"001",
              "userName":null,
              "permissions":["appMgt","sysMgmt","demoMgmt"],
              "menus":[
                {"id":5555555555,"funcCode":"appMgt","funcName":"应用管理","funcType":1,"useScene":0,"parentId":0,"parentIds":"0","orderNo":0,"status":1,"title":null,"icon":null,"descr":null,"createdTime":1599515328000,"createdUser":0,"modifiedTime":1599515328000,"modifiedUser":0,"children":[]},
                {"id":325916311546880,"funcCode":"test","funcName":"test","funcType":1,"useScene":1,"parentId":0,"parentIds":"null,0","orderNo":0,"status":1,"title":null,"icon":null,"descr":null,"createdTime":1642338825000,"createdUser":251041056254976,"modifiedTime":1642338825000,"modifiedUser":null,"children":[]},
                {"id":87025901000000,"funcCode":"demoMgmt","funcName":"系统管理","funcType":1,"useScene":0,"parentId":0,"parentIds":"0","orderNo":999,"status":1,"title":null,"icon":null,"descr":null,"createdTime":1547130137000,"createdUser":null,"modifiedTime":1547130137000,"modifiedUser":null,"children":[{"id":87025901004000,"funcCode":"demo","funcName":"功能设置","funcType":1,"useScene":0,"parentId":87025901000000,"parentIds":"87025901000000","orderNo":0,"status":1,"title":null,"icon":null,"descr":null,"createdTime":1599148678000,"createdUser":0,"modifiedTime":1599148678000,"modifiedUser":0,"children":[]},{"id":87025901001000,"funcCode":"sysOrg","funcName":"机构管理","funcType":1,"useScene":0,"parentId":87025901000000,"parentIds":"87025901000000","orderNo":1,"status":1,"title":null,"icon":null,"descr":null,"createdTime":1547130309000,"createdUser":null,"modifiedTime":1547130309000,"modifiedUser":null,"children":[]},{"id":319678278437888,"funcCode":"sysManageGroup","funcName":"管理组","funcType":1,"useScene":0,"parentId":87025901000000,"parentIds":"0,87025901000000","orderNo":5,"status":1,"title":null,"icon":null,"descr":null,"createdTime":1639292909000,"createdUser":251041056254976,"modifiedTime":1639292956000,"modifiedUser":251041056254976,"children":[]},{"id":319679170141184,"funcCode":"sysGrantAuth","funcName":"用户授权","funcType":1,"useScene":0,"parentId":87025901000000,"parentIds":"0,87025901000000","orderNo":7,"status":1,"title":null,"icon":null,"descr":null,"createdTime":1639293344000,"createdUser":251041056254976,"modifiedTime":1639293344000,"modifiedUser":null,"children":[]},{"id":321413569604608,"funcCode":"sysBatchRole","funcName":"批量角色","funcType":1,"useScene":0,"parentId":87025901000000,"parentIds":"0,87025901000000","orderNo":9,"status":1,"title":null,"icon":null,"descr":null,"createdTime":1640140220000,"createdUser":251041056254976,"modifiedTime":1640140220000,"modifiedUser":null,"children":[]},{"id":87025901002000,"funcCode":"sysUser","funcName":"用户管理","funcType":1,"useScene":0,"parentId":87025901000000,"parentIds":"87025901000000","orderNo":10,"status":1,"title":null,"icon":null,"descr":null,"createdTime":1547130915000,"createdUser":null,"modifiedTime":1547130915000,"modifiedUser":null,"children":[]},{"id":87025901003000,"funcCode":"sysRole","funcName":"角色管理","funcType":1,"useScene":0,"parentId":87025901000000,"parentIds":"87025901000000","orderNo":20,"status":1,"title":null,"icon":null,"descr":null,"createdTime":1547131264000,"createdUser":null,"modifiedTime":1547131264000,"modifiedUser":null,"children":[]},{"id":87025901008000,"funcCode":"logManage","funcName":"日志管理","funcType":1,"useScene":0,"parentId":87025901000000,"parentIds":"87025901000000","orderNo":30,"status":1,"title":null,"icon":null,"descr":null,"createdTime":1547131264000,"createdUser":null,"modifiedTime":1547131264000,"modifiedUser":null,"children":[]}]}],
              "isSuperadmin":"1",
              "userOrgIds":"1000030006,1000030006"
            }*/
            // sessionStorage.setItem('userInfo', JSON.stringify(aaa))
            location.reload()
          }
        } else {
          window.console.log('error submit!!')
          return false
        }
      })
    },
    // 储存表单信息
    setUserInfo: function() {
      // 判断用户是否勾选记住密码，如果勾选，向cookie中储存登录信息，
      // 如果没有勾选，储存的信息为空
      if (this.loginForm.remember) {
        this.setCookie('username', this.loginForm.username)
        // base64加密密码
        const password = Base64.encode(this.loginForm.password)
        this.setCookie('password', password)
        this.setCookie('remember', this.loginForm.remember)
      } else {
        this.setCookie('username', '')
        this.setCookie('password', '')
      }
    },
    // 获取cookie
    getCookie: function(key) {
      if (document.cookie.length > 0) {
        var start = document.cookie.indexOf(key + '=')
        if (start !== -1) {
          start = start + key.length + 1
          var end = document.cookie.indexOf(';', start)
          if (end === -1) end = document.cookie.length
          return unescape(document.cookie.substring(start, end))
        }
      }
      return ''
    },
    // 保存cookie
    setCookie: function(cName, value, expiredays) {
      var exdate = new Date()
      exdate.setDate(exdate.getDate() + expiredays)
      document.cookie =
        cName +
        '=' +
        decodeURIComponent(value) +
        (expiredays == null ? '' : ';expires=' + exdate.toGMTString())
    }
  },
  async created() {
    const { projectCode, projectId } = this.$route.query
    if (this.$route.query && this.$route.query.token) {
      const params = {
        projectCode,
        projectId
      };
      const res = await loginApi.getUserInfoByToken(params,
        { 'Authorization': this.$route.query.token })
      if (res.code === '00000000') {
        sessionStorage.setItem('userInfo', JSON.stringify(res.data))
        location.reload()
      } else {
        this.$message.error(res.msg || '请重新登录')
      }
    }
    // 在页面加载时从cookie获取登录信息
    const username = this.getCookie('username')
    const password = Base64.decode(this.getCookie('password'))
    // 如果存在赋值给表单，并且将记住密码勾选
    if (username) {
      this.loginForm.username = username
      this.loginForm.password = password
      this.loginForm.remember = true
    }
    this.containeWith = document.body.clientWidth
    window.onresize = () => {
      this.containeWith = document.body.clientWidth
    }
  },
  async mounted() {
    debugger
    if (this.$route.query && this.$route.query.projectCode) {
      sessionStorage.setItem('projectCode', this.$route.query.projectCode)
    }
    document.onkeydown = ev => {
      var event = ev || event
      if (event.keyCode == 13) {
        this.handleLogin()
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #000;

img {
  display: block;
  margin: 20vh auto 0;
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 8px 5px 8px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px white inset;
      }
    }
  }
  .el-form-item {
    background: #ffffff;
    border-radius: 5px;
    color: #aaabaf;
  }
  .el-form-item__content {
    border: 1px solid #78797b;
  }
  .password {
    margin-top: 30px;
  }
  .el-button {
    margin-top: 30px;
    margin-bottom: 10px;
  }
  .el-form-item__error {
    margin-top: 4px;
  }
  .el-button--mini {
    font-size: 16px;
    font-weight: 600;
  }
  .el-checkbox {
    color: #919295;
  }
  .el-button--primary {
    color: #fff;
    background-color: #2b6ed3;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background: url('../../assets/images/bg.jpg');
  background-size: 100% 100%;
  min-width: 1360px;
  .qr-code-wrap {
    position: absolute;
    top: 5px;
    left: 50px;
    z-index: 1;
    margin: 30px 0 0 20px;
    cursor: pointer;
    .qr-code-text {
      font-size: 18px;
      color: rgba(234, 235, 237, 1);
      display: inline-block;
      width: 160px;
      height: 30px;
    }
    .erweima {
      font-size: 20px;
    }
    .small-img {
      vertical-align: middle;
    }
    .scan-canvas-wrap {
      position: absolute;
      top: 35px;
      left: 0;
      border: 1px solid #ccc;
      background: #fff;
      width: 140px;
      height: 140px;
      padding: 10px;
      box-sizing: border-box;
      .iconurl {
        border: 1px solid #fff;
        padding: 3px;
        border-radius: 3px;
        position: absolute;
        width: 30px;
        height: 30px;
        left: 50%;
        top: 50%;
        transform: translate(-50%, -50%);
      }
    }
    .scan-img-wrap {
      position: absolute;
      top: 35px;
      left: 160px;
      border: 1px solid #ccc;
      background: #fff;
      width: 140px;
      height: 140px;
      img {
        width: 130px;
        height: 130px;
        margin: 5px;
      }
    }
  }
  .content {
    height: 450px;
    width: 100%;
    position: absolute;
    top: 45%;
    transform: translateY(-50%);
  }

  .logo-wrap {
    position: absolute;
    left: 140px;
    border-radius: 5px;

    h2 {
      font-size: 40px;
      font-weight: 400;
      color: rgba(252, 253, 255, 1);
      margin: 18px 0 8px;
    }
    .describe {
      font-size: 20px;
      color: rgba(252, 253, 255, 1);
      margin-top: 0;
    }
  }
  .form-wrap {
    position: absolute;
    right: 200px;
    background-color: #fff;
    width: 400px;
    min-width: 350px;
    height: 420px;
    box-sizing: border-box;
    .main {
      position: absolute;
      box-sizing: border-box;
      padding: 0 30px 15px 30px;
      width: 100%;
      top: 48%;
      transform: translateY(-50%);
    }
    h3 {
      color: #2b6ed3;
      font-size: 24px;
      padding-bottom: 5px;
      text-align: center;
      margin-top: 30px;
    }
  }
  .login-form {
    width: 100%;
    box-sizing: border-box;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #78797b;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
    .key-icon {
      width: 24px;
      height: 24px;
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .set-language {
      color: #fff;
      position: absolute;
      top: 5px;
      right: 0px;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 28px;
  }
  .copyright {
    font-size: 16px;
    margin-top: 80px;
    color: rgba(252, 253, 255, 1);
    // text-align: center;
    // position: absolute;
    width: 100%;
    bottom: 20px;
  }

  @media screen and (max-width: 1500px) {
    .qr-code-wrap {
      font-size: 16px;
    }
    .logo-wrap {
      h2 {
        font-size: 32px;
      }
      .describe {
        font-size: 18px;
      }
      .copyright {
        font-size: 14px;
        margin-top: 50px;
      }
    }
    .form-wrap {
      width: 340px;
      right: 150px;
    }
  }
  .login-tip {
    font-size: 12px;
    font-weight: 400;
    color: rgba(77, 77, 77, 1);
  }
}
</style>
